#include "myhead.h"

#define DISP_BUF_SIZE (1000 * 600)

int main(void)
{
    lv_init();

    //打开液晶屏驱动，获取液晶屏参数信息，内存映射得到液晶屏首地址
    fbdev_init();

    /*数组屏幕缓冲区*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    //初始化数组和结构体
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*设置液晶屏分辨率，显示像素点的函数进行认为设置*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf; //把存放画面数据的
    disp_drv.flush_cb   = fbdev_flush;//在指定区间填充颜色值
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);//把信息注册到LVGL里面

    //开始触摸屏
    evdev_init();
    //定义触摸屏初始化，存放触摸屏信息
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); 
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    indev_drv_1.read_cb = evdev_read; //函数：获取触摸屏坐标
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

    ui_style_init();//初始化字体

    chat();

    while(1) {
        lv_timer_handler();
        usleep(5000);
    }
    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
